#include "stdafx.h"
#include "../Interface/ConsoleAppender.h"
#include <Windows.h>
#include <wincon.h>
#include <iostream>
#include <iomanip>

#include "../../CommonFile/stringencode.h"

namespace SimpleLog
{
	CConsoleAppender::CConsoleAppender()
		: m_file(nullptr)
	{
	}
	
	CConsoleAppender::~CConsoleAppender()
	{
		
	}

	void CConsoleAppender::outputLog(const wchar_t*  lpszContent, const LogLevel & logLevel, const wchar_t*  lpszTimeStamp
		, const unsigned long & uThreadID)
	{
		std::cout << std::setw(10) << wcs2mbs(GetLevelName(logLevel)).c_str();
		std::cout << std::setw(30) << wcs2mbs(lpszTimeStamp).c_str();
		std::cout << std::setw(10) << uThreadID;
		std::cout << wcs2mbs(lpszContent).c_str() << std::endl;
	}

	bool CConsoleAppender::initAppender()
	{
		BOOL bRet = AllocConsole();
		freopen_s(&m_file, "CONOUT$", "w", stdout);
		std::cout.flags(std::ios::left);
		std::cout << std::setw(10) << "level";
		std::cout << std::setw(30) << "time";
		std::cout << std::setw(10) << "ThreadID";
		std::cout << "content" << std::endl;

		return true;
	}

	bool CConsoleAppender::uninitAppender()
	{
		fclose(m_file);
		m_file = nullptr;
		FreeConsole();

		return true;
	}
}